package com.qn.config;

import cn.hutool.core.util.ObjectUtil;
import com.qn.entity.I18nMsg;
import com.qn.service.I18nMsgService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Locale;

/**
 * 自定义消息源
 *
 * @author QN
 * @date 2025/01/03
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DynamicMessageSource extends AbstractMessageSource {

    private final I18nMsgService I18nMsgService;

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        I18nMsg i18nMessage = I18nMsgService.getI18nMsg(locale, code);
        if (ObjectUtil.isNotEmpty(i18nMessage)) {
            return createMessageFormat(i18nMessage.getMsg() , locale);
        }
        return null;
    }

}
